package com.hanbitexam.action;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.hanbitexam.dao.QuestionDao;
import com.hanbitexam.vo.QuestionVo;

public class QuestionAction {
	int idx,type;



	QuestionVo vo;
	//String ex1,ex2,ex3,ex4;
	String[] examlist;
	int c_idx;
	public int getC_idx() {
		return c_idx;
	}

	public void setC_idx(int c_idx) {
		this.c_idx = c_idx;
	}

	String example,writer,answer,question;
	List<QuestionVo> list;
	
	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}	

	public QuestionVo getVo() {
		return vo;
	}

	public void setVo(QuestionVo vo) {
		this.vo = vo;
	}

	public String[] getExamlist() {
		return examlist;
	}

	public void setExamlist(String[] examlist) {
		this.examlist = examlist;
	}

	public int getIdx() {
		return idx;
	}

	public void setIdx(int idx) {
		this.idx = idx;
	}

	public List<QuestionVo> getList() {
		return list;
	}

	public void setList(List<QuestionVo> list) {
		this.list = list;
	}

	public String getExample() {
		return example;
	}

	public void setExample(String example) {
		this.example = example;
	}

	public String getWriter() {
		return writer;
	}

	public void setWriter(String writer) {
		this.writer = writer;
	}

	public String getAnswer() {
		return answer;
	}

	public void setAnswer(String answer) {
		this.answer = answer;
	}

	public String getQuestion() {
		return question;
	}

	public void setQuestion(String question) {
		this.question = question;
	}

	public String add(){	//문제추가
		QuestionVo vo = new QuestionVo();
		vo.setAnswer(answer);
		vo.setQuestion(question);
		vo.setWriter(writer);
		vo.setExample(example);
		vo.setC_idx(c_idx);
		vo.setType(type);
		System.out.println("answer :"+answer);
		System.out.println("question :"+question);
		System.out.println("writer :"+writer);
		System.out.println("example :"+ example);
		System.out.println("c_idx :"+c_idx);
		
		//QuestionDao dao = new QuestionDao();
		
		
		try {
//			dao.startTransaction();
			
			
			QuestionDao.insertQuestion(vo);			
			
		} catch (Exception e) {
			e.printStackTrace();
			
			return "fail";
		} finally {
//			try {
//				dao.endTransaction();
//			} catch (SQLException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
		}
		
		return "success";
	}
	
	public String allLoad(){
		System.out.println(" +++ AllQuestionAction's load() +++");
		System.out.println(" category'idx : "+idx);
		//QuestionDao dao = new QuestionDao();
		try {
			list = QuestionDao.selectQuestionByCategory(idx);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "success";
	}
	
	public String load(){
		System.out.println(" +++ QuestionAction's load() +++");
		//QuestionDao dao = new QuestionDao();
		try{
			vo = QuestionDao.selectQuestion(idx);
			examProcess(vo.getExample());
		}catch(Exception e){
			e.printStackTrace();
		}
		return "success";
	}
	
	public String modify(){	//문제 수정을 위한 메서드
		//이루어 되야 하는 동작 : 원래문제의 last값을 1에서 0으로 변경시키고
		//새로운 자료를 하나 추가한다.
		//
		
		System.out.println(" +++ QuestionAction's modify() +++");		
		//QuestionDao dao=new QuestionDao();
		QuestionVo vo = new QuestionVo();
		vo.setIdx(idx);	
		vo.setWriter(writer);
		vo.setExample(example);
		vo.setAnswer(answer);		
		vo.setQuestion(question);
		vo.setC_idx(c_idx);
		vo.setType(type);
	
		try {
			//dao.startTransaction();
			QuestionDao.updateLast(idx);
			QuestionDao.insertQuestion(vo);
			//dao.endTransaction();
		} catch (Exception e) {
			e.printStackTrace();
			return "fail";
		}
		
		return "success";
	}
	
	public String delete(){
		System.out.println(" +++ QuestionAction's delete() +++");
		//QuestionDao dao = new QuestionDao();
		try {
			System.out.println("idx : "+idx);
			//dao.deleteQuestion(idx);
			QuestionDao.updateLast(idx);
		} catch (Exception e) {
			e.printStackTrace();
			return "fail";
		}
		return "success";
	}


	public void examProcess(String str){	//보기항목을 처리하는메소드
		//String[] aa;
		examlist=str.split("!@");
		//System.out.println(aa.length);	
	}
	
	public String selecttype(){
		return "success";
	}

}
